package org.logsin37.tool.eurekamanager.api.controller;

import lombok.AllArgsConstructor;
import org.logsin37.tool.eurekamanager.domain.entity.EurekaApplication;
import org.logsin37.tool.eurekamanager.domain.entity.EurekaInstance;
import org.logsin37.tool.eurekamanager.domain.service.EurekaRemoteDomainService;
import org.logsin37.tool.eurekamanager.infra.constant.EurekaInstanceStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Eureka Manage Controller
 *
 * @author logsin37 2020/01/19 15:35
 */
@RestController
@RequestMapping("/eureka/manage/apps")
@AllArgsConstructor
public class EurekaApplicationController {

    private final EurekaRemoteDomainService eurekaRemoteDomainService;

    /**
     * 查询所有Eureka信息
     *
     * @param eurekaCode eureka代码
     * @return 查询结果
     */
    @GetMapping
    public Flux<EurekaApplication> listAllApplications(@RequestParam String eurekaCode){
        return this.eurekaRemoteDomainService.listAllApplications(eurekaCode);
    }

    /**
     * 将实例置为上线
     *
     * @param eurekaCode eureka代码
     * @param instance Eureka实例信息, app/instanceId不能为空
     * @return 操作结果
     */
    @PutMapping("/put-status/up")
    public Mono<Void> putStatusUp(@RequestParam String eurekaCode, @RequestBody EurekaInstance instance){
        return this.eurekaRemoteDomainService.putStatus(eurekaCode, instance, EurekaInstanceStatus.UP);
    }

    /**
     * 将实例置为下线
     *
     * @param eurekaCode eureka代码
     * @param instance Eureka实例信息, app/instanceId不能为空
     * @return 操作结果
     */
    @PutMapping("/put-status/out-of-service")
    public Mono<Void> putStatusOutOfService(@RequestParam String eurekaCode, @RequestBody EurekaInstance instance){
        return this.eurekaRemoteDomainService.putStatus(eurekaCode, instance, EurekaInstanceStatus.OUT_OF_SERVICE);
    }

}
